package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.Appointment;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.mapper.AppointmentMapper;
import cn.wolfcode.car.business.mapper.StatementMapper;
import cn.wolfcode.car.business.query.AppointmentQuery;
import cn.wolfcode.car.business.service.IAppointmentService;
import cn.wolfcode.car.business.service.IStatementItemService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class AppointmentServiceImpl implements IAppointmentService {

    @Autowired
    private AppointmentMapper appointmentMapper;
    @Autowired
    private StatementMapper statementMapper;

    @Override
    public TablePageInfo<Appointment> query(AppointmentQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<Appointment>(appointmentMapper.selectForList(qo));
    }


    @Override
    public void save(Appointment appointment) {
        appointment.setCreateTime(new Date());
        appointmentMapper.insert(appointment);
    }

    @Override
    public Appointment get(Long id) {
        return appointmentMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(Appointment appointment) {
        //根据id查询数据
        Appointment dbAppointment = this.get(appointment.getId());
        //如果该数据状态不是在预约中的状态进行修改操作, 则抛出异常
        if (!(Appointment.STATUS_APPOINTMENT.equals(dbAppointment.getStatus()))){
            throw new BusinessException("非法操作");
        }
        //如果数据状态没有问题则可以修改, 有些字段和数据使不允许给修改的
        appointmentMapper.updateByPrimaryKey(appointment);
    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            //根据id获取状态是否是预约中的状态
            //如果不是则抛出异常
            Appointment dbAppointment = this.get(dictId);
            if (Appointment.STATUS_ARRIVAL.equals(dbAppointment.getStatus())
                || Appointment.STATUS_SETTLE.equals(dbAppointment.getStatus())){
                throw new BusinessException("非法操作");
            }
            appointmentMapper.deleteByPrimaryKey(dictId);
        }
    }

    @Override
    public List<Appointment> list() {
        return appointmentMapper.selectAll();
    }

    @Override
    public void arrivalStop(Long id) {
        //根据id获取数据
        Appointment dbAppointment = this.get(id);
        //判断客户是否是预约中的状态 如果不是则抛出异常
        if (!(Appointment.STATUS_APPOINTMENT.equals(dbAppointment.getStatus()))){
            throw new BusinessException("非法操作");
        }
        //修改状态为到店状态, 并且添加到店时间
        appointmentMapper.changeArrivalStatus(id,Appointment.STATUS_ARRIVAL);
    }

    @Override
    public void cancel(Long id) {
        //根据id获取数据
        Appointment dbAppointment = this.get(id);
        //判断用户是否是预约中的状态,如果不是则抛出异常
        if (!(Appointment.STATUS_APPOINTMENT.equals(dbAppointment.getStatus()))){
            throw new BusinessException("非法操作");
        }
        //修改状态为用户取消
        appointmentMapper.changeStatus(id,Appointment.STATUS_CANCEL);
    }

    @Override
    public Long generateStatement(Long id) {
        //根据id查询出预约单的数据
        Appointment appointment = this.get(id);

        //查询服务单中的数据是否存在该预约单的服务结算单
        Statement statement = statementMapper.queryByAppintmentId(id);
        if (statement==null){
            statement = new Statement();
            //如果不存在则将预约单的数据封装到服务单对象中插入到服务单的表中返回id;
            //设置服务类型
            statement.setServiceType(Long.parseLong(appointment.getServiceType()+""));
            //设置车牌号码
            statement.setLicensePlate(appointment.getLicensePlate());
            //设置备注信息
            statement.setInfo(appointment.getInfo());
            //设置客户电话
            statement.setCustomerPhone(appointment.getCustomerPhone());
            //设置客户姓名
            statement.setCustomerName(appointment.getCustomerName());
            //设置车辆类型
            statement.setCarSeries(appointment.getCarSeries());
            //设置预约单的id
            statement.setAppointmentId(id);
            //设置到店时间
            statement.setActualArrivalTime(appointment.getActualArrivalTime());
            //设置创建订单时间
            statement.setCreateTime(new Date());
            //将数据插入到表中
            statementMapper.insert(statement);
            //修改预约单的状态
            appointmentMapper.changeStatus(id,Appointment.STATUS_SETTLE);
        }
        //如果存在则返回
        return statement.getId();
    }


}
